home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OTFindSerialPorts / OTFindSerialPorts.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.4 KB  |  149 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        OTFindSerialPorts.c
  3.  
  4.     Contains:    Sample to show how to find all the serial ports using OT.
  5.  
  6.     Written by: Quinn "The Eskimo!"    
  7.  
  8.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. /////////////////////////////////////////////////////////////////////
  25. // The OT debugging macros in <OTDebug.h> require this variable to
  26. // be set.
  27.  
  28. #ifndef qDebug
  29. #define qDebug    1
  30. #endif
  31.  
  32. /////////////////////////////////////////////////////////////////////
  33. // Pick up all the standard OT stuff.
  34.  
  35. #include <OpenTransport.h>
  36.  
  37. /////////////////////////////////////////////////////////////////////
  38. // Pick up device type definitions.
  39.  
  40. #include <OpenTptLinks.h>
  41.  
  42. /////////////////////////////////////////////////////////////////////
  43. // Pick up special APIs for getting port information.
  44.  
  45. #include <OpenTptConfig.h>
  46.  
  47. /////////////////////////////////////////////////////////////////////
  48. // Pick up the OTDebugBreak and OTAssert macros.
  49.  
  50. #include <OTDebug.h>
  51.  
  52. /////////////////////////////////////////////////////////////////////
  53. // Standard C prototypes.
  54.  
  55. #include <stdio.h>
  56.  
  57. /////////////////////////////////////////////////////////////////////
  58. // OTDebugStr is not defined in any OT header files, but it is
  59. // exported by the libraries, so we define the prototype here.
  60.  
  61. extern pascal void OTDebugStr(const char* str);
  62.  
  63. /////////////////////////////////////////////////////////////////////
  64.  
  65. static OSStatus PrintSerialPortInfo(const OTPortRecord *portRecord)
  66.     // Prints information about the port with the given portRecord.
  67. {
  68.     Str255 userVisibleName;
  69.     
  70.     // OTGetUserPortNameFromPortRef is a little known routine
  71.     // from <OpenTptConfig.h> that allows you to get a user
  72.     // visible name for an Open Transport port.  You must
  73.     // be running PPC code if you want to call this on a PPC machine.
  74.     
  75.     OTGetUserPortNameFromPortRef(portRecord->fRef, userVisibleName);
  76.     
  77.     printf("Found a serial port with port reference $%08lx:\n", portRecord->fRef);
  78.     printf("  User visible name is                       “%#s”.\n", userVisibleName);
  79.     printf("  String to pass to OTCreateConfiguration is “%s”.\n",  portRecord->fPortName);
  80.     printf("  Name of provider module is                 “%s”.\n",  portRecord->fModuleName);
  81.     printf("\n");
  82.     
  83.     return kOTNoError;
  84. }
  85.  
  86. static OSStatus OTFindSerialPorts(void)
  87.     // Lists all of the serial ports on the machine using Open Transport.
  88. {
  89.     OSStatus err;
  90.     Boolean portValid;
  91.     SInt32 portIndex;
  92.     OTPortRecord portRecord;
  93.     UInt16 deviceType;
  94.  
  95.     // Start portIndex at 0 and call OTGetIndexedPort until
  96.     // we find there are no more ports.
  97.         
  98.     portIndex = 0;
  99.     err = kOTNoError;
  100.     do {
  101.         portValid = OTGetIndexedPort(&portRecord, portIndex);
  102.         if (portValid) {
  103.         
  104.             // For each valid port, get the deviceType and, if
  105.             // it's a serial port and not an alias, call PrintSerialPort
  106.             // to dump out its information.  Note that you don't want
  107.             // to include aliases to the serial ports in the list, otherwise
  108.             // a standard machine will have 3 serial ports, "serialA", "serialB"
  109.             // and "serial".
  110.         
  111.             deviceType = OTGetDeviceTypeFromPortRef(portRecord.fRef);
  112.             if (deviceType == kOTSerialDevice && 
  113.                         (portRecord.fInfoFlags & kOTPortIsAlias) == 0) {
  114.                 err = PrintSerialPortInfo(&portRecord);
  115.             }
  116.         }
  117.         portIndex += 1;
  118.     } while ( portValid && err == kOTNoError);
  119.  
  120.     return err;
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////
  124.  
  125. void main(void)
  126. {
  127.     OSStatus err;
  128.     
  129.     printf("OTFindSerialPorts\n");
  130.     printf("-- Lists all the serial ports on the machine using OT\n");
  131.     printf("\n");
  132.     
  133.     err = InitOpenTransport();
  134.     
  135.     if (err == noErr) {
  136.     
  137.         err = OTFindSerialPorts();
  138.         
  139.         CloseOpenTransport();
  140.     }
  141.     
  142.     if (err == noErr) {
  143.         printf("Success.\n");
  144.     } else {
  145.         printf("Failed with error %d.\n", err);
  146.     }
  147.     printf("Done.  Press command-Q to Quit.\n");
  148. }
  149.